$(function(){
	var intername=$(".conLeft li.bg").children('.liRight').children('.intername').text();
	LocalCacheUtil.set("chat_avatar_"+intername, $(".conLeft li.bg").children('.liLeft').children('img').attr("src"));
	//加载 历史聊天内容
	setTimeout(function(){
		var chatHtm = LocalCacheUtil.get("chat_"+intername);
		$('.newsList').html(chatHtm);
		$('.RightCont').scrollTop($('.RightCont')[0].scrollHeight );
	}, 500);

	//绑定回车搜索
	$('#dope').bind('keypress',function(event){
		//log("keyCode:"+event.keyCode);
		var keyword = $(this).val();
		if(event.keyCode == "13" && CommUtil.isNotEmpty(keyword)){
			$('.sendBtn').trigger('click')
			return false;
		}
	});

});

$('.conLeft li').on('click',function(){
	//缓存 聊天内容
	var lastUserName = $(this).siblings(".bg").find(".liRight .intername").text();
	if($('.newsList').html().length > 0){
		LocalCacheUtil.set("chat_"+lastUserName, $('.newsList').html());
	}

	$(this).addClass('bg').siblings().removeClass('bg');
	var intername=$(this).children('.liRight').children('.intername').text();
	LocalCacheUtil.set("chat_avatar_"+intername, $(this).children('.liLeft').children('img').attr("src"));
	$('.headName').text("分类："+intername);
	$('.newsList').html('');
	searchAnswer('');

	//加载 历史聊天内容
	setTimeout(function(){
		var chatHtm = LocalCacheUtil.get("chat_"+intername);
		$('.newsList').html(chatHtm);
	}, 500);

})
$('.sendBtn').on('click',function(){
	var news=$('#dope').val();
	var intername = $(".conLeft li.bg").children('.liRight').children('.intername').text();
	if(news==''){
		alert('不能为空');
	}else if(intername==''){
		dialogLeftTopTip("请先指定分类", "danger")
	} else{
		$('#dope').val('');
		var str='';
		str+='<li>'+
				'<div class="nesHead"><img src="'+LocalCacheUtil.get("chat_avatar_"+intername)+'"/></div>'+
				'<div class="news">'+news+'</div>'+
			'</li>';
		$('.newsList').append(str);

		//缓存 聊天内容
		if($('.newsList').html().length > 0){
			LocalCacheUtil.set("chat_"+intername, $('.newsList').html());
		}

		searchAnswer(news);
		if(news.length > 8){
			news = news.substring(0, 8)+"...";
		}
		$('.conLeft').find('li.bg').children('.liRight').children('.infor').text(news);
		$('.RightCont').scrollTop($('.RightCont')[0].scrollHeight );
	}
});

function answers(answerText){
	var answer='';
	answer+='<li>'+
				'<div class="answerHead"><img src="'+ctxStatic+'/chat/img/avatar/robot.jpg"/></div>'+
				'<div class="answers">'+answerText+'</div>'+
			'</li>';
	$('.newsList').append(answer);	
	$('.RightCont').scrollTop($('.RightCont')[0].scrollHeight );
}
$('.ExP').on('mouseenter',function(){
	$('.emjon').show();
})
$('.emjon').on('mouseleave',function(){
	$('.emjon').hide();
})
$('.emjon li').on('click',function(){
	var intername=$(".conLeft li.bg").children('.liRight').children('.intername').text();
	var imgSrc=$(this).children('img').attr('src');
	var str="";
	str+='<li>'+
			'<div class="nesHead"><img src="'+LocalCacheUtil.get("chat_avatar_"+intername)+'"/></div>'+
			'<div class="news"><img class="Expr" src="'+imgSrc+'"></div>'+
		'</li>';
	$('.newsList').append(str);
	$('.emjon').hide();
	var arr=["有种别斗图！","我最美我最美","咱不识别您的表情","您能输入文字吗，文字我懂！", "我不管，我最帅，我是你的小可爱", "突然想开个车", "咱不知道您要表达什么！"];
	var answerText = arr[Math.floor((Math.random()*arr.length))];
	setTimeout(function(){
		answers(answerText);
	}, 500);
	$('.RightCont').scrollTop($('.RightCont')[0].scrollHeight );
})

LocalCacheUtil = {
	get: function(key){
		return window.localStorage.getItem(key);
	},
	set: function(key, val){
		return window.localStorage.setItem(key, val);
	},
	has: function(key){
		var obj = this.get(key);
		return obj!=null;
	},
	remove: function remove(key){
		return window.localStorage.removeItem(key);
	}
}
